<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseAdminController.php';
require_once 'CMS/AdminCommon.php';

//Người tạo : Hoàng Hải Luân - Team 0
//Module 	: LanguageManager
//Chức năng	: Quản lý language 
class Admin_LanguageController extends BaseAdminController {
	private $error = array();

	public function init(){
		parent::init();
		$this->_helper->layout()->setLayout('layoutadmin');
		$user = new User();
		$user->init();
		$user->check_login();
		$user->permission();
		$this->count = 0;
	}
		
	public function indexAction() 
	{
		$tbLanguage = new Admin_Model_Language();
		$languages = $tbLanguage->getAllLanguages();
		$data = array();
		if($languages != NULL && count($languages) != 0){
			foreach($languages as $lang){
				$action = array();
				$action[] = array(
					'text' => 'Edit',
					'href' => '/admin/language/edit?language_id=' . $lang['language_id']
				);				
				$data[] = array(
						'language_id'	=> $lang['language_id'],
						'name'			=> $lang['name'],
						'code'			=> $lang['code'],
						'status'		=> $lang['status'],
						'selected'		=> isset($this->request->post['selected']) && in_array($result['language_id'], $this->request->post['selected']),
						'default'		=> $lang['language_default'],
						'action'      	=> $action				
						);
			}
		}
		if (isset($_SESSION['success'])) 
		{
			$this->view->success = $_SESSION['success'];
			unset($_SESSION['success']);
		} 
		else 
		{
			$this->view->success = '';
		}

		if (isset($_SESSION['error'])) 
		{
			$this->view->error_warning = $_SESSION['error'];
			unset($_SESSION['error']);
		} 
		else 
		{
			$this->view->error_warning = '';
		}
		$this->view->languages 	= $data;
		$this->view->insert 	= "/admin/language/insert";
		$this->view->delete 	= "/admin/language/delete";
	}

	public function editAction()
	{
		if($this->_request->isPost() && isset($_GET['language_id']))
		{
			if(isset($_POST['name']) && isset($_POST['code']) && isset($_POST['image']) && isset($_POST['status']))
			{
				if($this->validateForm())
				{
					$tbLanguage = new Admin_Model_Language();
					if(isset($_POST['language_default']) && $_POST['language_default'] == 1)
					{
						if ( $tbLanguage->editDefaultLanguage() != 1)
						{
							$_SESSION['error'] = "Warning: Update has error.Please try again !";	
							$this->_redirect("admin/language");						
						}					
					}
					if ( $tbLanguage->editLanguage($_GET['language_id'], $_POST) != 1)
					{
						$_SESSION['error'] = "Warning: Update has error.Please try again !";	
						$this->_redirect("admin/language");						
					}
					else
					{
						$_SESSION['success'] = "Success: You have modified languages!";	
						$this->_redirect("admin/language");						
					}											
				}
				else
				{
			 		if (isset($this->error['name'])) 
			 		{
						$this->view->error_name = $this->error['name'];
					} 
					else 
					{
						$this->view->error_name = '';
					}
			
			 		if (isset($this->error['code'])) 
			 		{
						$this->view->error_code = $this->error['code'];
					} 
					else 
					{
						$this->view->error_code = '';
					}
					
			 		if (isset($this->error['image'])) 
			 		{
						$this->view->error_image = $this->error['image'];
					} 
					else 
					{
						$this->view->error_image = '';
					}
					
					if (isset($this->error['status'])) 
					{
						$this->view->error_status = $this->error['status'];
					} 
					else 
					{
						$this->view->error_status = '';
					}					
					$language = array(
									'language_id'	=> $_GET['language_id'],
									'name'			=> $_POST['name'],
									'code'			=> $_POST['code'],
									'status'		=> $_POST['status'],
									'image'			=> $_POST['image'],
									'default'		=> $_POST['language_default']						
								);
					$this->view->cancel = "/admin/language";
					$this->view->action = "/admin/language/edit?language_id=".$_GET['language_id'];								
					$this->view->language = $language;																			
				}
			}
			else
			{
				$this->_redirect("admin/language");
			}
		}
		else if(isset($_GET['language_id']))
		{
			$tbLanguage = new Admin_Model_Language();
			$id = $_GET['language_id'];
			$data = $tbLanguage->getLanguageById($id);
			if($data == NULL || count($data) != 1){
				$this->_redirect("admin/language");
			}
			
			foreach ($data AS $lang){
				$language = array(
						'language_id'	=> $lang['language_id'],
						'name'			=> $lang['name'],
						'code'			=> $lang['code'],
						'status'		=> $lang['status'],
						'image'			=> $lang['image'],
						'default'		=> $lang['language_default']						
					);
			}
			$this->view->cancel = "/admin/language";
			$this->view->action = "/admin/language/edit?language_id=".$id;
			$this->view->language = $language;
		}
		else
		{
			$this->_redirect("admin/language");
		}
	}
		
	public function insertAction() {
			if($this->_request->isPost()){
			if(isset($_POST['name']) && isset($_POST['code']) && isset($_POST['image']) && isset($_POST['status']))
			{
				if($this->validateForm())
				{
					$tbLanguage = new Admin_Model_Language();
					if(isset($_POST['language_default']) && $_POST['language_default'] == 1)
					{
						if ( $tbLanguage->editDefaultLanguage() != 1)
						{
							$_SESSION['error'] = "Warning: Update has error.Please try again !";	
							$this->_redirect("admin/language");						
						}					
					}
					if ( $tbLanguage->insertLanguage($_POST) != 1)
					{
						$_SESSION['error'] = "Warning: Update has error.Please try again !";	
						$this->_redirect("admin/language");						
					}
					else
					{
						$_SESSION['success'] = "Success: You have modified languages!";	
						$this->_redirect("admin/language");						
					}											
				}
				else
				{
			 		if (isset($this->error['name'])) 
			 		{
						$this->view->error_name = $this->error['name'];
					} 
					else 
					{
						$this->view->error_name = '';
					}
			
			 		if (isset($this->error['code'])) 
			 		{
						$this->view->error_code = $this->error['code'];
					} 
					else 
					{
						$this->view->error_code = '';
					}
					
			 		if (isset($this->error['image'])) 
			 		{
						$this->view->error_image = $this->error['image'];
					} 
					else 
					{
						$this->view->error_image = '';
					}
					
					if (isset($this->error['status'])) 
					{
						$this->view->error_status = $this->error['status'];
					} 
					else 
					{
						$this->view->error_status = '';
					}					
					$language = array(
							'name'			=> $_POST['name'],
							'code'			=> $_POST['code'],
							'status'		=> $_POST['status'],
							'image'			=> $_POST['image'],
							'default'		=> $_POST['language_default']						
							);
					$this->view->cancel = "/admin/language";
					$this->view->action = "/admin/language/insert";								
					$this->view->language = $language;																			
				}
			}
			else
			{
				$this->_redirect("admin/language");
			}
		}
		else
		{
			$language = array(
					'name'			=> '',
					'code'			=> '',
					'status'		=> '1',
					'image'			=> '',
					'default'		=> '0'						
					);
			$this->view->cancel = "/admin/language";
			$this->view->action = "/admin/language/insert";								
			$this->view->language = $language;
		}
	}

	public function deleteAction() 
	{
		$tbLanguage = new Admin_Model_Language();
		$tbMenu = new Admin_Model_Menu();	
		if ($this->_request->isPost() && isset($_POST['selected']) && $this->validateDelete()) 
		{
			foreach ($_POST['selected'] as $language_id){ 
				if($tbMenu->deleteAllMenuItem($language_id) == 1)
				{
					$tbLanguage->deleteLanguage($language_id);
				}				
			}
			$_SESSION['success'] = "Success: You have modified languages!";
		}
		$this->_redirect("admin/language");
	}

	private function validateForm() {
		if ((strlen(utf8_decode($_POST['name'])) < 3) || (strlen(utf8_decode($_POST['name'])) > 32)) 
		{
			$this->error['name'] = "Language Name must be between 3 and 32 characters";
		}

		if (strlen(utf8_decode($_POST['code'])) < 2) 
		{
			$this->error['code'] = "Language Code must at least 2 characters!";
		}
		
		if ((strlen(utf8_decode($_POST['image'])) < 3) || (strlen(utf8_decode($_POST['image'])) > 32)) 
		{
			$this->error['image'] = "Image Filename must be between 3 and 64 characters!";
		}

		if(isset($_POST['language_default']) && $_POST['language_default'] == '1' && $_POST['status'] == 0)
		{
			$this->error['status'] = "Default language can't be disabled!";
		}
		if (!$this->error) 
		{
			return TRUE;
		} 
		else 
		{
			return FALSE;
		}
	}
	private function validateDelete() 
	{
		if ($this->_request->isPost() && isset($_POST['selected'])) 
		{
			$tbLanguage = new Admin_Model_Language();
			foreach ($_POST['selected'] as $language_id) 
			{
			$data = $tbLanguage->getLanguageById($language_id);	
				if(isset($data) && count($data == 1)){
					foreach ($data as $lang){
						if($lang['language_default'] == '1'){
							$_SESSION['error'] = "Warning: ".$lang['name']." is defaut language so it cannot be deleted !";
							return FALSE;
						}
					}
				}
			}
		return TRUE;
		}
		else
		{
			return FALSE;
		}		
	}	
}
?>